﻿Public Class frmBienesMuebles
    Dim utilidades As New Utilidades
    Dim _titulo As String
    Dim _form As frmClientes
    Dim _tipoVentana As String
    Dim _sePresionoGuardar As Boolean = False
    Dim _componentesHabilitados As Boolean
    Public _idCliente As String
    Public _secuenciaMueble As String

    Public ID_CLIENTE2 As String
    Public PATENTE As String
    Public ID_TIPO_MUEBLE As String
    Public TIPO As String
    Public ID_MARCA As String
    Public MARCA As String
    Public MODELO As String
    Public ANYO As String
    Public VALOR_MONETARIO As String
    Public SEC1 As String

    Public Sub New(ByVal tipoVentana As Integer, ByRef form As frmClientes)
        Me.InitializeComponent()

        If tipoVentana = Constantes.Ventana_Tipo_Apertura_Nuevo Then
            Me.Text = "Nuevo Bien Mueble"
            _componentesHabilitados = True
        End If
        If tipoVentana = Constantes.Ventana_Tipo_Apertura_Edicion Then
            Me.Text = "Modificar Bien Mueble"
            _componentesHabilitados = True
        End If
        If tipoVentana = Constantes.Ventana_Tipo_Apertura_Detalle Then
            Me.Text = "Detalle de Bien Mueble"
            _componentesHabilitados = False
        End If

        _form = form
        _tipoVentana = tipoVentana
    End Sub

    

    Private Sub frmBienesMuebles_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myAccesoDatos As New AccesoDatos
        myAccesoDatos.traer_valores_combo(Me.cmbTipoMueble, Constantes.Tabla_TIPO_BIEN_MUEBLE)
        myAccesoDatos.traer_valores_combo(Me.cmbMarca, Constantes.Tabla_MARCAS_VEHICULOS)

        If _tipoVentana = Constantes.Ventana_Tipo_Apertura_Nuevo Then
            ''Me.cargarDatosNuevoMueble()
        End If
        If _tipoVentana = Constantes.Ventana_Tipo_Apertura_Edicion Then
            Me.cargarDatosMueble()
        End If
        If _tipoVentana = Constantes.Ventana_Tipo_Apertura_Detalle Then
            Me.cargarDatosMueble()
            habilitacionComponentesDetalle()
        End If

        Me.mostrarBotones()
    End Sub

    Private Sub cargarDatosMueble()
        Me.txtModelo.Text = MODELO
        Me.txtPatente.Text = PATENTE
        Me.txtValorAproximado.Text = VALOR_MONETARIO
        Me.txtAnno.Text = ANYO

        Me.cmbMarca.SelectedValue = Convert.ToString(ID_MARCA)
        Me.cmbTipoMueble.SelectedValue = Convert.ToString(ID_TIPO_MUEBLE)
    End Sub
    Private Sub mostrarBotones()
        Me.cmdAceptar.Visible = (Not _componentesHabilitados)
        Me.cmdGuardar.Visible = _componentesHabilitados
        Me.cmdCancelar.Visible = _componentesHabilitados
    End Sub

    Private Sub cmdAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAceptar.Click
        _sePresionoGuardar = True
        Me.Close()
    End Sub

    Private Sub txtValorAproximado_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValorAproximado.KeyPress
        utilidades.validarKeyPressNumerico(e)
    End Sub

    Private Sub txtAnno_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAnno.KeyPress
        utilidades.validarKeyPressNumerico(e)
    End Sub
    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        _sePresionoGuardar = False
        Me.Close()
    End Sub
    Private Sub frmBienesMuebles_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If _sePresionoGuardar = False Then
            If MessageBox.Show("Si cancela no se guardarán los cambios ¿Está seguro?", "Atención", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                e.Cancel = False
            Else
                e.Cancel = True
            End If
        End If
    End Sub

   
    Private Sub cmdGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGuardar.Click
        If Me.cmbTipoMueble.SelectedIndex < 1 Then
            MsgBox("El tipo de Bien es requerido.", vbOKOnly + vbCritical, "Atención")
            cmbTipoMueble.Focus()
            Exit Sub
        End If
        If Me.cmbMarca.SelectedValue < 1 Then
            MsgBox("La Marca es requerida.", vbOKOnly + vbCritical, "Atención")
            cmbMarca.Focus()
            Exit Sub
        End If
        If Me.txtModelo.Text.Trim() = "" Then
            MsgBox("El Modelo es requerido.", vbOKOnly + vbCritical, "Atención")
            Me.txtModelo.Focus()
            Exit Sub
        End If
        If Me.txtPatente.Text.Trim() = "" Then
            MsgBox("La Patente es requerida.", vbOKOnly + vbCritical, "Atención")
            Me.txtPatente.Focus()
            Exit Sub
        End If
        If Me.txtValorAproximado.Text.Trim() = "" Then
            MsgBox("El Valor Aprox. es requerido.", vbOKOnly + vbCritical, "Atención")
            Me.txtValorAproximado.Focus()
            Exit Sub
        End If
        If Me.txtAnno.Text.Trim() = "" Then
            MsgBox("El Año es requerido.", vbOKOnly + vbCritical, "Atención")
            Me.txtAnno.Focus()
            Exit Sub
        End If
        If Me.txtAnno.Text.Trim().Length < 4 Then
            MsgBox("Año inválido.", vbOKOnly + vbCritical, "Atención")
            Me.txtAnno.Focus()
            Exit Sub
        End If
        If Convert.ToInt32(Me.txtAnno.Text.Trim()) > Convert.ToInt32(Format(Today, "yyyy")) Then
            MsgBox("Año inválido.", vbOKOnly + vbCritical, "Atención")
            Me.txtAnno.Focus()
            Exit Sub
        End If

        Me.ID_CLIENTE2 = _idCliente
        Me.PATENTE = Me.txtPatente.Text.Trim
        Me.ID_TIPO_MUEBLE = Me.cmbTipoMueble.SelectedValue
        Me.TIPO = Me.cmbTipoMueble.Text.Trim
        Me.ID_MARCA = Me.cmbMarca.SelectedValue
        Me.MARCA = Me.cmbMarca.Text.Trim
        Me.MODELO = Me.txtModelo.Text.Trim
        Me.ANYO = Me.txtAnno.Text.Trim
        Me.VALOR_MONETARIO = Me.txtValorAproximado.Text.Trim



        If _form.existeMueble(PATENTE, SEC1) = False Then
            Me._form.actualizarGrillaMuebles(ID_CLIENTE2, PATENTE, ID_TIPO_MUEBLE, _
                TIPO, ID_MARCA, MARCA, MODELO, ANYO, VALOR_MONETARIO, SEC1, _tipoVentana)
            _sePresionoGuardar = True
            Me.Close()
        Else
            MsgBox("El Bien Mueble ya existe.", vbOKOnly + vbCritical, "Atención")
        End If
    End Sub

    Private Sub habilitacionComponentesDetalle()
        Me.lblAsterisco.Enabled = False
        Me.lblDatosRequeridos.Enabled = False
        Me.Label1.Enabled = False
        Me.Label7.Enabled = False
        Me.Label9.Enabled = False
        Me.Label40.Enabled = False
        Me.Label5.Enabled = False
        Me.Label10.Enabled = False

        Me.txtModelo.Enabled = False
        Me.txtPatente.Enabled = False
        Me.txtValorAproximado.Enabled = False
        Me.txtAnno.Enabled = False

        Me.cmbMarca.Enabled = False
        Me.cmbTipoMueble.Enabled = False
    End Sub
End Class